
function give_offer(offer, fb_id)
{
	var $dialog = $('<div></div>')
		.html('Are you sure you want to give this offer to this person?')
		.dialog({
			modal: 'true',
			title : 'Give Offer',
			buttons : 
			{
				'Yes' : function()
						{
							$.ajax({
								url : "giveoffer/",
								type : 'post',
								data :
								{
									offer_id : offer,
									fb : fb_id,
								},
								success : function(response)
								{
									window.location.reload();
								}
							});
						},
				'No' : function()
						{
							$(this).dialog('close');
						}
			}
		});
}

function reject_requester(offer)
{
	var $dialog = $('<div></div>')
		.html('Are you sure you want to cancel your reservation of your item to this person?')
		.dialog({
			modal: 'true',
			title : 'Cancel reservation',
			buttons : 
			{
				'Yes' : function()
						{
							$.ajax({
								url : "rejectrequester/",
								type : 'post',
								data :
								{
									offer_id : offer,
								},
								success : function(response)
								{
									window.location.reload();
								}
							});
						},
				'No' : function()
						{
							$(this).dialog('close');
						}
			}
		});
}

function accept_offer(offer)
{
	var $dialog = $('<div></div>')
		.html('Are you sure you have received the item and want to close this transaction?')
		.dialog({
			modal: 'true',
			title : 'Accept Request',
			buttons : 
			{
				'Yes' : function()
						{
							$.ajax({
								url : "acceptoffer/",
								type : 'post',
								data :
								{
									offer_id : offer,
								},
								success : function(response)
								{
									window.location.reload();
								}
							});
						},
				'No' : function()
						{
							$(this).dialog('close');
						}
			}
		});
}

function cancel_accepted_offer(offer)
{
	var $dialog = $('<div></div>')
		.html('This item has been offered to you. Are you sure you want to reject it?')
		.dialog({
			modal: 'true',
			title : 'Cancel Request',
			buttons : 
			{
				'Yes' : function()
						{
							$.ajax({
								url : "canceloffer/",
								type : 'post',
								data :
								{
									status : 'accepted',
									offer_id : offer,
								},
								success : function(response)
								{
									window.location.reload();
								}
							});
						},
				'No' : function()
						{
							$(this).dialog('close');
						}
			}
		});
}

function cancel_pending_offer(offer)
{
	var $dialog = $('<div></div>')
		.html('Are you sure you want to cancel this request?')
		.dialog({
			modal: 'true',
			title : 'Cancel Request',
			buttons : 
			{
				'Yes' : function()
						{
							$.ajax({
								url : "canceloffer/",
								type : 'post',
								data :
								{
									status : 'pending',
									offer_id : offer,
								},
								success : function(response)
								{
									window.location.reload();
								}
							});
						},
				'No' : function()
						{
							$(this).dialog('close');
						}
			}
		});
}

function request_offer(offer, status)
{
	switch(status)
	{
		case "Available":
			var $dialog = $('<div></div>')
				.html('Are you sure you want to make an request for this offer?')
				.dialog({
					modal: 'true',
					title : 'Request Item',
					buttons : 
					{
						'Yes' : function()
								{
									$.ajax({
										url : "requestoffer/",
										type : 'post',
										data :
										{
											status : 'available',
											offer_id : offer,
										},
										success : function(response)
										{
											window.location.reload();
										}
									});
								},
						'No' : function()
								{
									$(this).dialog('close');
								}
					}
				});
			break;
		case "Reserved":
			var $dialog = $('<div></div>')
				.html('This item has been reserved to someone else. Are you sure you want to make a request for it?')
				.dialog({
					modal: 'true',
					title : 'Request Item',
					buttons : 
					{
						'Yes' : function()
								{
									$.ajax({
										url : "requestoffer/",
										type : 'post',
										data :
										{
											status : 'reserved',
											offer_id : offer,
										},
										success : function(response)
										{
											window.location.reload();
										}
									});
								},
						'No' : function()
								{
									$(this).dialog('close');
								}
					}
				});
			break;
		default:
	}
}

var lasttimestamp;

function submitComment(id, fb_id)
{
	var commentText = document.getElementById('comment-field');
	$.ajax({
		url : "submitcomment/",
		type		: 'post',
		data :
		{
			last_timestamp : lasttimestamp,
			offer_id : id,
			comment : commentText.value,
		},
		dataType : 'json',
		success : function(response)
		{
			html = '';
			for(var i=0;i<response.length;i++)
			{
				html += '<div class="single-comment-container" id = "comment_' + response[i].chat_id + '">';
				html += '<div class="facebook-info">';
				html += '<span class="get-fb-pic">' + response[i].poster_fb_id + '</span>';
				html += '</div>';
				html += '<div class="comment-content">';
				html += '<div class = "datetime small-text">'+response[i].date_created+'</div>';
				html += '<span class="get-fb-name">'+response[i].poster_fb_id+'</span> says: <br />';
				html += '<div class = "message">'+response[i].message.replace(/\n/g, '<br />');+'</div>';
				if(response[i].poster_fb_id == fb_id){
					html+= '<br /><a class = "action-button action-delete" href = "Javascript:void(0);" onclick="deleteComment('+response[i].chat_id+ ')" /></a>';
				}
				html += '</div>';
				html += '<div style="clear: both"></div>';
				html += '</div>';
			}
			if (response)
				lasttimestamp = response[response.length-1].date_created; 
			$('.new-comment').before(html);
			replaceIdByName();
			replaceIdByPic();
		}
	});
}

function deleteComment(id)
{
	var $dialog = $('<div></div>')
		.html('Are you sure you want to delete this comment?')
		.dialog({
			modal: 'true',
			title : 'Delete Comment',
			buttons : 
			{
				'Yes' : function()
						{
							$.ajax({
								url : "deletecomment/"+id,
								type : 'post',
								data :
								{
									chat_id : id,
								},
								success : function (response)
								{
									$("#comment_"+id).hide();
								}
							});
							$(this).dialog('close');
						},
				'No' : function()
						{
							$(this).dialog('close');
						}
			}
		});
}

function shareFB(base_url)
{
	FB.ui(
	{
		method: 'feed',
		name: offer.item_title,
		link: base_url + 'itemdescription/' + offer.offer_id,
		picture: base_url + 'resources/offer_images_thumbnail/' + offer.item_pic,
		caption: 'I am offering this item up for grabs. Head over to The Trove for a look!',
		description: 'Description: ' +  offer.item_desc,
	},
	function(response) {
	if (response && response.post_id) {
		// Post published
		} else {
		// Post not published
		}
	}
);
}

